define(["require", "exports"], function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.storage = exports.http = void 0;
    exports.generateFlag = generateFlag;
    exports.generateId = generateId;
    exports.queryString = queryString;
    exports.getUrlParams = getUrlParams;
    exports.getPortsRotation = getPortsRotation;
    exports.calcDistance = calcDistance;
    exports.openWindow = openWindow;
    exports.showDialog = showDialog;
    exports.closeDialog = closeDialog;
    exports.getStorageValue = getStorageValue;
    let index = Math.floor(Math.random() * 10000);
    function generateFlag(prefix) {
        let flag;
        const flags = Array
            .from(document.querySelectorAll("[data-flag]"))
            .map(el => el.dataset.flag)
            .filter(f => !!f);
        do {
            flag = `${prefix}-${Math.floor(Math.random() * 100000)}`;
        } while (flags.includes(flag));
        return flag;
    }
    function generateId(prefix) {
        return `${prefix}-${Date.now().toString(36)}-${(index++).toString(36)}`;
    }
    function queryString(params) {
        return new URLSearchParams(params).toString();
    }
    function getUrlParams(url) {
        const res = {};
        const p = new URLSearchParams(url ?? location.search);
        p.forEach((v, k) => {
            res[k] = v;
        });
        return res;
    }
    function getPortPosition(p) {
        const rect = p.getBoundingClientRect();
        return {
            x: rect.left + rect.width / 2,
            y: rect.top + rect.height / 2
        };
    }
    function getPortsRotation(inport, outport) {
        const from = inport.classList.contains("in-port") ? inport : outport;
        const to = outport.classList.contains("out-port") ? outport : inport;
        const p1 = getPortPosition(from);
        const p2 = getPortPosition(to);
        const dx = p2.x - p1.x;
        const dy = p2.y - p1.y;
        const radians = Math.atan2(dy, dx);
        return (radians * (180 / Math.PI) + 360) % 360;
    }
    function calcDistance(holdNode, dst) {
        const x = dst.offsetLeft - holdNode.offsetLeft;
        const y = dst.offsetTop - holdNode.offsetTop;
        return Math.pow(x * x + y * y, 0.5);
    }
    class Http {
        async get(url) {
            const res = await fetch(url);
            return await res.json();
        }
        async post(url, data) {
            const params = data;
            const res = await fetch(url, {
                method: "post",
                body: params
            });
            return await res.json();
        }
    }
    exports.http = new Http();
    function openWindow(url, second = false) {
        const screenWidth = window.screen.width;
        const win = window.open(url, second ? "second_win" : "first_win", `width=450, height=520, top=100, left=${screenWidth - 520}`);
        if (!win) {
            alert('请允许弹出窗口！');
        }
        return win;
    }
    function showDialog(url, dialog, width = 400, height = 440) {
        const iframe = dialog.querySelector("iframe");
        iframe.style.width = `${width}px`;
        iframe.style.height = `${height}px`;
        iframe.src = url;
        const mask = document.querySelector(".mask");
        mask.style.display = "block";
        dialog.showModal();
    }
    function closeDialog(dialog) {
        const mask = document.querySelector(".mask");
        mask.style.display = "none";
        dialog.querySelector("iframe").src = "./view/blank.html";
        dialog.close();
    }
    function getStorageValue(key) {
        const str = exports.storage.getItem(key);
        let json;
        if (str) {
            json = JSON.parse(str);
        }
        return json;
    }
    exports.storage = (function () {
        const win = parent ?? opener;
        return win ? win.sessionStorage : sessionStorage;
    })();
});
